package com.tang.model.simple;

import java.util.Objects;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/8/15 11:51
 * @Desc
 * @Remarks
 */
public class FlyableFactory {
    /**
     * 方式1
     *
     * @param name
     * @return
     */
    public Flyable createFlyable(String name) {
        if (Objects.nonNull(name)) {
            if (name.equals("bird")) {
                return new Bird();
            } else if (name.equals("rocket")) {
                return new Rocket();
            }
        }
        return null;
    }

    /**
     * 方式2
     *
     * @param flyableEnum
     * @return
     */
    public Flyable createFlyableByEnum(FlyableEnum flyableEnum) {
        if (null == flyableEnum) {
            return null;
        }
        if (FlyableEnum.ROCKET == flyableEnum) {
            return new Rocket();
        } else if (FlyableEnum.BIRD == flyableEnum) {
            return new Bird();
        }
        return null;
    }

    /**
     * 方式3
     *
     * @param className
     * @return
     */
    public Flyable createFlyableByName(String className) {
        if (Objects.nonNull(className)) {
            try {
                Object instance = Class.forName(className).newInstance();
                return (Flyable) instance;
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
            }

        }
        return null;
    }

    /**
     * 方式4
     *
     * @param clazz
     * @return
     */
    public Flyable createFlyable(Class<? extends Flyable> clazz) {
        if (clazz != null) {
            try {
                Flyable instance = clazz.newInstance();
                return instance;
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
